define(['wiseMen/dataModel/QueryDefTpl','text!wiseMen/dataModel/QuerySelectorTpl.html'],function(QueryDefTpl,QuerySelectorTpl){
	webUtils.nameSpace("wiseMen.dataModel.QuerySelector");
	wiseMen.dataModel.QuerySelector=function(params){
		var me=this;
		var queryGrid;
		var hasSelectedGrid;
		me.params=$.extend({},{
			code:null,
			model:'view',
			selectModel:'multi',
			viewFieldCode:'title',
			hasSelected:[],
			onSelected:function(rows){
				
			}
		},params);
		
		var _$QuerySelectorTpl=$(QuerySelectorTpl);
		$("body").prepend(_$QuerySelectorTpl);
		var dialog =webUtils.dialog({
			title: '选项',
			content:_$QuerySelectorTpl.find(".j_dialog"),
			size:"w",
			onremove: function () {
                if (params.onClose) {
                    params.onClose.call(null);
                }
            },
            buttons: [{
            	label: '关闭',
                action: function (dialog) {
                    dialog.close();
                }
            },{
            	label: '确定',
            	icon: 'glyphicon glyphicon-check',
                cssClass: 'btn-primary',
                action: function (dialog) {
                	if(me.params.onSelected){
                		var rows=hasSelectedGrid.bootstrapTable("getData");
                		me.params.onSelected.call(this,rows);
                	}
                	
                    dialog.close();
                }
            }],
            onshown:function(dialog){
            	var body = dialog.getModalBody();
            	var j_search=_$QuerySelectorTpl.find(".j_search");
            	
            	queryGrid=new QueryDefTpl({
            		code:me.params.code,
        			model:me.params.model,
        			searchBarType:"dialog",
        			selectModel:me.params.selectModel,
        			toolBarDiv:body.find(".j_toolbar"),
        			scriptDiv:body.find(".j_script"),//引用脚本的div
        			searchDiv:j_search,//查询定义栏
        			gridDiv:body.find(".j_grid"),//grid表格
        			onSelect:function(selectRow,rowIndex){
        				var row=  hasSelectedGrid.bootstrapTable("getRowByUniqueId", selectRow.ID);
        		        
        		        if(!row){
        		        	if(me.params.selectModel=='single'){
        		        		hasSelectedGrid.bootstrapTable('removeAll');
        		            }
        		        	hasSelectedGrid.bootstrapTable("append", {
        		        		id:selectRow["ID"],
        		        		title:selectRow[me.params.viewFieldCode]
        		        	});
        		        }
        			}
                });
            	
            	hasSelectedGrid=initHasSelectedGrid(body.find(".j_hasSelectedGrid"),params.hasSelected);
            }
		});
	}
	
	var initHasSelectedGrid=function(gridSelector,hasSelected){
		
	    var grid = gridSelector.bootstrapTable({
	        columns: [
	                {title: '已选项', field: 'title', align: 'left',width:300}
	            ],
	        uniqueId:"id",
	        data:hasSelected,
	        sidePagination : 'client',
			pagination : false,
			height : $(window).height()*0.5,
			onClickRow : function(row,$tr) {
				grid.bootstrapTable("removeByUniqueId", row.id);
			}
	    });
	    return grid;
	}
	return wiseMen.dataModel.QuerySelector;
});